package com.itcast.yygh.orders.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itcast.yygh.orders.entity.OrderMessageLog;
import com.itcast.yygh.orders.mapper.OrderMessageLogMapper;
import com.itcast.yygh.orders.service.OrderMessageLogService;
import org.springframework.stereotype.Service;

@Service
public class OrderMessageLogServiceImpl extends ServiceImpl<OrderMessageLogMapper, OrderMessageLog>
        implements OrderMessageLogService {

    @Override
    public void saveMessageLog(Long orderId, String messageId, String messageContent) {
        OrderMessageLog log = new OrderMessageLog();
        log.setOrderId(orderId);
        log.setMessageId(messageId);
        log.setMessageContent(messageContent);
        log.setStatus(0); // 待发送
        log.setRetryCount(0);
        this.save(log);
    }

    @Override
    public void updateMessageStatus(String messageId, Integer status) {
        OrderMessageLog log = this.getByMessageId(messageId);
        if (log != null) {
            log.setStatus(status);
            this.updateById(log);
        }
    }

    @Override
    public OrderMessageLog getByMessageId(String messageId) {
        return this.getOne(new QueryWrapper<OrderMessageLog>().eq("message_id", messageId));
    }
}
